package com.example.park2212.merchant

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.example.lib_base.entity.MerchatBeanItem
import com.example.lib_base.mvvm.BaseActivity
import com.example.park2212.R
import com.example.park2212.databinding.ActivityMerchantBinding
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
@Route(path = "/app/merchant/MerchantActivity")
class MerchantActivity : BaseActivity<ActivityMerchantBinding,MerchantViewModel>() {

    lateinit var merchantAdapter: MerchantAdapter
    val list = mutableListOf<MerchatBeanItem>()
    override fun initView() {
        binding.headMerchant.headerTitle.setText("商家入驻")
        binding.headMerchant.headerBack.setOnClickListener {
            finish()
        }

        binding.headMerchant.headerAdd.setOnClickListener {
            ARouter.getInstance().build("/app/merchant/MerchantAddActivity").navigation()
        }

        merchantAdapter = MerchantAdapter()
        binding.rvMerchant.adapter = merchantAdapter
        binding.rvMerchant.layoutManager = LinearLayoutManager(this)


        binding.merchantRvstatus.setOnCheckedChangeListener { radioGroup, i ->
            merchantAdapter.data.clear()
            if (i == R.id.merchant_a){
                merchantAdapter.addData(list.filter { it.status == "新建" })
            }else {
                merchantAdapter.addData(list.filter { it.status != "新建" })
            }
            merchantAdapter.notifyDataSetChanged()
        }
        merchantAdapter.addChildClickViewIds(R.id.merchant_status)
        merchantAdapter.setOnItemChildClickListener { adapter, view, position ->
            ARouter.getInstance().build("/app/merchant/MerchantDetailActivity")
                .withString("mid",merchantAdapter.data[position].id.toString()).navigation()
        }

    }

    override fun initData() {
        viewModel.merchantSuccess.observe(this){
            list.clear()
            merchantAdapter.data.clear()
           list.addAll(it)
            merchantAdapter.addData(it.filter { it.status == "新建" })
            merchantAdapter.notifyDataSetChanged()
        }
    }

    override fun onResume() {
        super.onResume()
        viewModel.getMerchant()
        binding.merchantRvstatus.check(R.id.merchant_a)
    }

}